import pandas as pd
import matplotlib.pyplot as plt

# 设置图片清晰度
plt.rcParams['figure.dpi'] = 300

# 设置中文字体
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

def generate_vehicle_freight_bar_chart():
    try:
        # 读取数据文件
        file_path = 'FhjlViewDD.xlsx'
        df = pd.read_excel(file_path)

        # 计算各车辆的总货运量
        vehicle_freight_totals = df.groupby('车辆')['净重'].sum()

        # 绘制柱状图
        plt.figure(figsize=(16, 8))
        vehicle_freight_totals.plot(kind='bar')
        plt.xlabel('车辆')
        plt.ylabel('总货运量')
        plt.title('6月份各车辆的总货运量柱状图')
        plt.xticks(rotation=90)

        # 保存图片
        plt.savefig('6月份各车辆的总货运量柱状图.png')
        print('柱状图已成功保存为 6月份各车辆的总货运量柱状图.png')
    except FileNotFoundError:
        print(f'错误：未找到文件 {file_path}，请检查文件路径。')
    except Exception as e:
        print(f'发生未知错误：{e}')

if __name__ == '__main__':
    generate_vehicle_freight_bar_chart()